package com.architecture.core.utils;

import com.architecture.core.except.ErrException;
import com.architecture.core.web.vo.BaseQueryVO;
import com.architecture.core.web.vo.PermissionQueryVO;
import com.architecture.core.web.vo.UserQueryVO;
import org.apache.commons.lang.StringUtils;

/**
 * <p>Project: BTC</p>
 * <p>Description: </p>
 * <p>Copyright (c) 2012 Wilmar Consultancy Services</p>
 * <p>All Rights Reserved.</p>
 *
 * @author <a href="mailto:hujianguang@wcs-global.com">胡建光</a>
 */
public final class HqlUtils {

    private static final String BASE_QUERY = "SELECT result FROM :entity result WHERE result.id <> null";

    /**
     * 查询用户列表
     *
     * @param userQueryVO
     * @return
     * @throws ErrException
     */
    public static final String assemblyUserHql(UserQueryVO userQueryVO,Class c) throws ErrException {

        if (null == userQueryVO) {
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        StringBuilder buff = new StringBuilder(BASE_QUERY.replace(":entity",c.getSimpleName()));

        HqlUtils.baseAssemblyHql(buff, userQueryVO);

        if (!StringUtils.isBlank(userQueryVO.getUsername())) {
            buff.append(" AND result.username like '%");
            buff.append(userQueryVO.getUsername().trim());
            buff.append("%'");
        }

        if(null != userQueryVO.getGroup()){
            buff.append(" AND result.group = ");
            buff.append(userQueryVO.getGroup());
        }

        if(null != userQueryVO.getType()){
            buff.append(" AND result.type = ");
            buff.append(userQueryVO.getType());
        }

        if(null != userQueryVO.getActive()){
            buff.append(" AND result.active = ");
            buff.append(userQueryVO.getActive());
        }

        if(null != userQueryVO.getLevel()){
            buff.append(" AND result.level = ");
            buff.append(userQueryVO.getLevel());
        }

        return buff.toString().trim();
    }

    /**
     * 查询用户列表
     *
     * @param permissionQueryVO
     * @return
     * @throws ErrException
     */
    public static String assemblyPermissionHql(PermissionQueryVO permissionQueryVO,Class c) throws ErrException{

        if(null == permissionQueryVO){
            throw new ErrException(ErrCode.NULL_OBJ);
        }

        StringBuilder buff = new StringBuilder(BASE_QUERY.replace(":entity",c.getSimpleName()));

        HqlUtils.baseAssemblyHql(buff, permissionQueryVO);

        if(!StringUtils.isBlank(permissionQueryVO.getName())){
            buff.append(" AND result.name like '%");
            buff.append(permissionQueryVO.getName());
            buff.append("%'");
        }

        return buff.toString().trim();  //To change body of created methods use File | Settings | File Templates.
    }

    private static void baseAssemblyHql(StringBuilder buff, BaseQueryVO baseQueryVO) throws ErrException{

        if (null != baseQueryVO.getStartCreateTime()) {
            buff.append(" AND result.createTime >= '");
            buff.append(TimeUtils.getTimeString(baseQueryVO.getStartCreateTime())) ;
            buff.append("'");
        }

        if (null != baseQueryVO.getEndCreateTime()) {
            buff.append(" AND result.createTime <= '" );
            buff.append(TimeUtils.getTimeString(baseQueryVO.getEndCreateTime()));
            buff.append("'");
        }

        if (null != baseQueryVO.getStartUpdateTime()) {
            buff.append(" AND result.updateTime >= '");
            buff.append( TimeUtils.getTimeString(baseQueryVO.getStartUpdateTime()));
            buff.append("'");
        }

        if (null != baseQueryVO.getEndUpdateTime()) {
            buff.append(" AND result.updateTime <= '");
            buff.append(TimeUtils.getTimeString(baseQueryVO.getEndUpdateTime()));
            buff.append("'");
        }

        if (null != baseQueryVO.getStartDeleteTime()) {
            buff.append(" AND result.deleteTime >= '");
            buff.append(TimeUtils.getTimeString(baseQueryVO.getStartDeleteTime()));
            buff.append("'");
        }

        if (null != baseQueryVO.getEndDeleteTime()) {
            buff.append(" AND result.deleteTime <= '");
            buff.append(TimeUtils.getTimeString(baseQueryVO.getEndDeleteTime()));
            buff.append("'");
        }

        if(!StringUtils.isBlank(baseQueryVO.getCreateUser())){
            buff.append(" AND result.createUser like '%");
            buff.append(baseQueryVO.getCreateUser());
            buff.append("%'");
        }
        if(!StringUtils.isBlank(baseQueryVO.getUpdateUser())){
            buff.append(" AND result.updateUser like '%");
            buff.append(baseQueryVO.getUpdateUser());
            buff.append("%'");
        }

        if(!StringUtils.isBlank(baseQueryVO.getDeleteUser())){
            buff.append(" AND result.deleteUser like '%");
            buff.append(baseQueryVO.getDeleteUser());
            buff.append("%'");
        }

        if(!StringUtils.isBlank(baseQueryVO.getDesc())){
            buff.append(" AND result.desc like '%");
            buff.append(baseQueryVO.getDesc());
            buff.append("%'");
        }

        if(true == baseQueryVO.isQueryDel()){
            buff.append(" AND result.deleted = true");
        }
    }
}
